<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialCode">
              <a-input v-model="model.materialCode" :placeholder="materialCodePlaceholder"  :disabled="materialCodeDisabled"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialName">
              <a-input v-model="model.materialName" placeholder="请输入物料名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialTypeId">
              <j-search-select-tag v-model="model.materialTypeId" dict="mdm_material_type,material_type_name,id"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialUnitCode">
              <j-search-select-tag type="list" v-model="model.materialUnitCode" dict="mdm_unit,unit_name,unit_code" placeholder="请选择单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialSpec">
              <a-input v-model="model.materialSpec" placeholder="请输入规格"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="材质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialMaterial">
              <a-input v-model="model.materialMaterial" placeholder="请输入材质"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物料分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialClassId">
              <j-tree-select
                v-model="model.materialClassId"
                dict="mdm_material_class,material_class_name,id"
                pid-field="pid">
              </j-tree-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物料来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialSourceCode">
              <j-dict-select-tag type="list" v-model="model.materialSourceCode" dictCode="mdm_material_source" placeholder="请选择物料来源" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="采购员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialBuyerId">
              <j-select-user-by-dep v-model="model.materialBuyerId" :multi="false"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="启用标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableFlag">
              <j-switch v-model="model.enableFlag" :options="[1,0]"></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productPicture">
                  <j-upload v-model="model.materialPic" text="上传" fileType="image"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import {cloneObject, validateDuplicateValue} from '@/utils/util'

export default {
  name: 'MdmMaterialMainForm',
  components: {
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      materialCodeDisabled: false,
      materialCodePlaceholder: '请输入物料号',
      model:{
        enableFlag:"1",
      },
      validatorRules: {
        materialName: [
          { required: true, message: '请输入物料名称!'},
        ],
        materialTypeId: [
          { required: true, message: '请输入物料类型!'},
        ],
        materialUnitCode: [
          { required: true, message: '请输入单位!'},
        ],
      },
      url: {
        add: "/mdm/material/add",
        edit: "/mdm/material/edit",
        queryById: "/mdm/material/queryById",
        genMaterialCode: "/mdm/material/genMaterialCode",
        checkMaterialCodeGenerate: "/mdm/material/checkMaterialCodeGenerate",
      }
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    this.checkMaterialCodeGen();
  },
  methods: {
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          httpAction(httpurl,this.model,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    checkMaterialCodeGen(){
      let that = this;
      getAction(that.url.checkMaterialCodeGenerate, {}).then((res) => {
        if (res.success) {
          this.materialCodeDisabled = res.result
          if(this.materialCodeDisabled){
            this.materialCodePlaceholder = '物料号由系统自动生成';
          }else{
            this.materialCodePlaceholder = '请输入物料号';
            this.$set(this.validatorRules, 'materialCode',[{ required: true, message: '请输入物料号!'},] );
          }
        } else {
          that.$message.warning(res.message);
        }
      })
    },
    copy(record) {
      let copy = cloneObject(record);
      copy.id = null;
      copy.materialCode = null;
      copy.materialPic = null;
      this.model = Object.assign({}, copy);
      this.visible = true;
    },
  }
}
</script>

<style scoped>
</style>